﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_worksadd : System.Web.UI.Page
    {
        private int worksid = 0 ;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }

            imgPicUrl.ImageUrl = hfPicUrl.Value;

            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "ckfinder/";
            _FileBrowser.SetupCKEditor(txbContent);
        }
        private int InitFromUrl(string url, int first)
        {
            int rtn = first;
            try
            {
                rtn = Int32.Parse(Request.QueryString[url].ToString());
            }
            catch { }
            return rtn;
        }
        private void BindData()
        {
            BindWorksCategory();
            BindNotice();
            //BindTop();
        }
        private void BindWorksCategory()
        {
            List<WorksCategoryInfo> list = WorksCategoryInfoLogic.GetNowPageWorksCategoryInfo(20, 1);
            ddlCategory.DataSource = list;
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataBind();
        }
        private void BindWorksPicInfoFromList(List<WorksPicInfo> list)
        {
            WorksPicRpt.DataSource = list;
            WorksPicRpt.DataBind();
        }
        private void BindWorksInfo(int wid)
        {
            WorksInfo work = WorksInfoLogic.GetWorksInfo(wid);
            txbTitle.Text = work.Title;
            txbCustomerName.Text = work.CustomerName;
            txbContent.Text = work.WorkContent;
            txbTeamWorkTime.Text = work.TeamWorkTime;
            ddlCategory.SelectedValue = work.CategoryID.ToString();
        }

        private void BindNotice()
        {
            noticefail.Visible = false;
            noticesuccess.Visible = false;
        }
        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            WorksInfo work = createWorksInfo();
            List<WorksPicInfo> piclist = createWorksPicList();
            int isSuccess = WorksInfoLogic.AddWorksInfo(work);
            if (isSuccess == 1)
            {
                worksid = WorksInfoLogic.GetMaxID();
                foreach (var item in piclist)
                {
                    item.WorksID = worksid;
                    isSuccess += WorksPicInfoLogic.AddWorksPicInfo(item);
                }
                if (isSuccess == piclist.Count() + 1)
                {
                    ChangeStatus(true);
                    BindWorksPicInfoFromList(piclist);
                    return;
                }
            }
            ChangeStatus(false);
        }
        private WorksInfo createWorksInfo()
        {
            WorksInfo work = new WorksInfo();
            work.CategoryID = Int32.Parse(ddlCategory.SelectedValue);
            work.CustomerName = txbCustomerName.Text.Replace("'", "''");
            work.IsNWorks = 1;
            work.HotFlag = 0;
            work.PublishTime = DateTime.Now;
            work.TeamWorkTime = txbTeamWorkTime.Text.Replace("'", "''");
            work.Title = txbTitle.Text.Replace("'", "''");
            work.TopFlag = 0;//
            work.WorkContent = txbContent.Text.Replace("'", "''");
            work.ClickCount = 0;
            work.PicURL = hfPicUrl.Value;
            work.WorksBrief = txbWorksBrief.Text.Replace("'", "''");
            return work;
        }
        private List<WorksPicInfo> createWorksPicList()
        {
            List<WorksPicInfo> list = new List<WorksPicInfo>();
            if (Request.Form["picurl[]"] != null && Request.Form["piclevel[]"] != null)
            {
                string[] picurls = Request.Form["picurl[]"].Split(',');
                string[] piclevels = Request.Form["piclevel[]"].Split(',');
                string[] picnames = Request.Form["picname[]"].Split(',');
                //string top = Request.Form["headshow"];
                for (int i = 0; i < picurls.Count(); i++)
                {
                    WorksPicInfo wpi = new WorksPicInfo();
                    wpi.PicURL = picurls[i];
                    wpi.PicLevel = Int32.Parse(piclevels[i]);
                    wpi.PicName = picnames[i];
                    //if ((top == ""||top == null) && i==0)
                    //{
                    //    wpi.TopFlag = 1;
                    //}
                    //else if (wpi.PicURL == top)
                    //{
                    //    wpi.TopFlag = 1;
                    //}
                    //else
                    //{
                    //    wpi.TopFlag = 0;
                    //}
                    list.Add(wpi);
                }
            }
            return list;
        }
        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();divupload();", true);
        }
    }
}
